from django.core.cache import caches
from django.shortcuts import render
from .models import Book
from django.core.paginator import Paginator
from django.views.decorators.cache import cache_page
# Create your views here.

# 使用默认default内存缓存
# @cache_page(30)
def books_details(request):
    bs = Book.objects.all().order_by("id")
    # 使用默认default内存缓存
    # b = Book.objects.first()
    # print(b.title)


    # 使用redis缓存
    cache = caches["redis"]
    # 未设置缓存时查找
    # print(cache.get("malei"),"---------------------")
    # 设置缓存键值和有效时间40秒
    # cache.set("malei","滴滴",40)
    # 再次查找（有效时间内为 滴滴     40秒之后为None）
    print(cache.get("malei"),"---------------------")

    paginator = Paginator(bs,10)
    page_num = request.GET.get("page")
    page = paginator.get_page(page_num)
    return render(request,"main/books_details.html",context=({
        "page":page,
    }))
